<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property string $uid
 * @property string $name
 * @property string $password
 * @property string $mail
 * @property string $url
 * @property string $screenName
 * @property string $created
 * @property string $activated
 * @property string $logged
 * @property string $group
 * @property string $authCode
 */
class Users extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{users}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name,mail', 'unique'),
			array('password', 'required','on'=>'insert'),
			array('name, screenName,password', 'length', 'max'=>32),
			array('mail, url', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('uid, name, password, mail, url, screenName, created, activated, logged, group, authCode', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'uid' => 'Uid',
			'name' => 'Name',
			'password' => 'Password',
			'mail' => 'Mail',
			'url' => 'Url',
			'screenName' => 'Screen Name',
			'created' => 'Created',
			'activated' => 'Activated',
			'logged' => 'Logged',
			'group' => 'Group',
			'authCode' => 'Auth Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('uid',$this->uid,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('mail',$this->mail,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('screenName',$this->screenName,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('activated',$this->activated,true);
		$criteria->compare('logged',$this->logged,true);
		$criteria->compare('group',$this->group,true);
		$criteria->compare('authCode',$this->authCode,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord){
				$this->created=time();
				$this->authCode=$this->generateAuthCode();
				$this->password=$this->hashPassword($this->password,$this->authCode);
			}elseif($this->password!=Users::model()->findByPk($this->uid)->password){
					$this->authCode=$this->generateAuthCode();
					$this->password=$this->hashPassword($this->password,$this->authCode);
				
			}
			return true;
		}
		else
			return false;
	}

	/*
	 * validatePassword
	 * @param string $password
	 * @return boolean
	 */
	public function validatePassword($password)
	{
		return $this->hashPassword($password,$this->authCode)===$this->password;
	}

	/*
	 * @param string $password
	 * @param string $authCode
	 */
	public function hashPassword($password,$authCode)
	{
		return md5($authCode.$password);
	}

	/*
	 */
	protected function generateAuthCode()
	{
		return uniqid('',true);
	}
}
